package com.example.demo.controller;


import com.example.demo.Service.CommentService;
import com.example.demo.Service.StarService;
import com.example.demo.Service.PostService;
import com.example.demo.entity.Comment;
import com.example.demo.entity.Star;
import com.example.demo.entity.Post;
import com.example.demo.entity.User;
import com.example.demo.repository.CommentRepository;
import com.example.demo.repository.StarRepository;
import com.example.demo.repository.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/stars")
public class StarController {
    @Autowired
    private StarService starService;

    @Autowired
    private StarRepository starRepository;

    @Autowired
    private PostService postService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private PostRepository postRepository;

    @Autowired
    private CommentRepository commentRepository;

    //收藏
    @PostMapping
    public ResponseEntity<?> createStar(@RequestBody Star star) {
        try {
            //检查收藏对象类型并进行相应的处理
            if (star.getPost() != null) {
                //点赞对象是Post
                Post post = postRepository.findById(star.getPost().getId())
                        .orElseThrow(() -> new IllegalArgumentException("Post not found"));
                //关联找到的Post到Star对象
                star.setPost(post);
            } else {
                //如果不是对Post的收藏，则抛出异常
                throw new IllegalArgumentException("Like must be for a post or a comment.");
            }

            //保存Star对象
            Star savedStar = starService.save(star);
            return ResponseEntity.ok(savedStar);
        } catch (IllegalArgumentException e) {
            //返回错误响应
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        } catch (Exception e) {
            //捕获其他错误
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred while saving the like.");
        }
    }

    //取消收藏
    @DeleteMapping("/{id}")
    public void deleteStar(@PathVariable Long id){
        starService.deleteById(id);
    }

    //获取文章的收藏数
    @GetMapping("/post/count/{postid}")
    public Long countStarsByPost(@PathVariable Long postid) {
        Post post = new Post();
        post.setId(postid);
        if (post == null || post.getId() == null) {
            throw new IllegalArgumentException("Post not found");
        }
        return starService.countStarsByPost(post);
    }

    //检查用户是否已经对文章收藏
    @GetMapping("/post/{postId}/user/{userId}/hasStared")
    public boolean hasStaredPost(@PathVariable Long postId, @PathVariable Long userId) {
        Post post = new Post();
        post.setId(postId);

        User user = new User();
        user.setId(userId);

        return starRepository.existsByPostAndUser(post, user);
    }
}
